package hua.mtreader.feature.bookshelf.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.animation.OvershootInterpolator;
import android.widget.RelativeLayout;
import android.widget.ScrollView;

import com.nineoldandroids.animation.ValueAnimator;

import hua.mtreader.common.utils.ToastAlone;

/**
 * Created by Administrator on 2017/3/17.
 */

public class MyScrollView extends ScrollView {


    private int originHeight;
    private BookshelfTopView rlRencent;
    private RelativeLayout rlRencentBg;
    private int intrinsicHeight;

    private OnRefreshListener onRefreshListener;
    private int maxHeight;
    /** 超过该高度才能刷新 **/
    private int upDataHeight;

    public MyScrollView(Context context) {
        super(context);
    }

    public MyScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MyScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }


    public void setTopViewHeight(BookshelfTopView rlRencent) {
        this.rlRencent = rlRencent;
        rlRencentBg = rlRencent.getRlRencentBg();
        intrinsicHeight = rlRencentBg.getBackground().getIntrinsicHeight();
        maxHeight = (int) (intrinsicHeight * 1.4f);
        originHeight = rlRencentBg.getHeight();
        upDataHeight = maxHeight - maxHeight / 4;
    }


    @Override
    protected boolean overScrollBy(int deltaX, int deltaY, int scrollX, int scrollY, int scrollRangeX, int scrollRangeY, int maxOverScrollX, int maxOverScrollY, boolean isTouchEvent) {

        // 1.getScrollY() <= 0 在顶部
        // 2.是否是由手指触摸超出的，是则为true, 如果是惯性,则为false
        // 3.deltaY : Y轴的瞬时变化量, 向下拉时是负值，向上拉是正值
        if(getScrollY() <= 0 && isTouchEvent && deltaY < 0){
            int newHeight = rlRencentBg.getHeight() + Math.abs(deltaY);
            if(newHeight < maxHeight){
                rlRencentBg.getLayoutParams().height = newHeight;
                rlRencentBg.requestLayout();
            }
        }
        return super.overScrollBy(deltaX, deltaY, scrollX, scrollY, scrollRangeX, scrollRangeY, maxOverScrollX, maxOverScrollY, isTouchEvent);
    }


    @Override
    public boolean onTouchEvent(MotionEvent ev) {


        switch (ev.getAction()) {
            case MotionEvent.ACTION_UP:
                final int startValue = rlRencentBg.getHeight();
                ValueAnimator valueAnimator = ValueAnimator.ofInt(1);
                // 使用 nineoldandroids-2.4.0.jar 可以向下兼容
                valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                    @Override
                    public void onAnimationUpdate(ValueAnimator animation) {
                        float percent = animation.getAnimatedFraction();
                        Integer evaluate = evaluate(percent, startValue, originHeight);
                        rlRencentBg.getLayoutParams().height = evaluate;
                        rlRencentBg.requestLayout();

                        if (percent == 1.0f && onRefreshListener != null && upDataHeight < startValue) {
                            onRefreshListener.onRefresh();
                            ToastAlone.showShortToast("刷新..");
                        }
                    }
                });
                valueAnimator.setInterpolator(new OvershootInterpolator(2));
                valueAnimator.setDuration(300);
                valueAnimator.start();
                break;
        }
        return super.onTouchEvent(ev);
    }

    /**
     * 该方法copy IntEvaluator.java
     * @param fraction
     * @param startValue
     * @param endValue
     * @return
     */
    public Integer evaluate(float fraction, Integer startValue, Integer endValue) {
        int startInt = startValue;
        return (int)(startInt + fraction * (endValue - startInt));
    }

    public interface OnRefreshListener{
        void onRefresh();
    }

    public OnRefreshListener getOnRefreshListener() {
        return onRefreshListener;
    }

    public void setOnRefreshListener(OnRefreshListener onRefreshListener) {
        this.onRefreshListener = onRefreshListener;
    }
}
